<?php


namespace app\common\service;
use app\common\RootService;
use edward\captcha\facade\CaptchaApi;

class CaptchaService extends RootService
{
    /**
     * 生成无状态图形验证码
     * @return \think\response\Json
     */
    public function createImg()
    {
        $captchaData = CaptchaApi::create();
        $captchaData['base64'] = preg_replace('/\r\n/', '', $captchaData['base64']);
        unset($captchaData['code']);
        return $captchaData;
    }

    /**
     * 生成无状态手机验证码
     * @param string $mobile
     * @return array|object
     */
    public function createSms(string $mobile)
    {
        $smsData = CaptchaApi::createSms($mobile);
        unset($smsData['code']);
        return $smsData;
    }


    /**
     * 图形验证码校验
     * @param string $code
     * @param string $key
     * @return bool
     */
    public function verifyImg(string $code, string $key)
    {
        if (CaptchaApi::check($code,$key)) {
            return true;
        }
        return false;
    }

    /**
     * 手机验证码校验
     * @param string $code
     * @param string $key
     * @return bool
     */
    public function verifySms(string $code, string $key)
    {
        if (CaptchaApi::checkSms($code, $key)) {
            return true;
        }
        return false;
    }
}